package com.qpx.springbootdemo1.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qpx.springbootdemo1.SpringbootDemo1Application;
import com.qpx.springbootdemo1.config.UserConfig;
import com.qpx.springbootdemo1.dto.UserDto;
import com.qpx.springbootdemo1.entity.User;
import com.qpx.springbootdemo1.service.IUserService;
import com.qpx.springbootdemo1.vo.ResultEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2019-11-29
 */
@Controller
@RequestMapping("/user")
public class UserController {

    Logger logger = LoggerFactory.getLogger(SpringbootDemo1Application.class);


    @Autowired
    IUserService userService;


    @Autowired
    private UserConfig userConfig;


    @RequestMapping(method = RequestMethod.GET,value = "/getUserPage")
    public String  getUserPage(UserDto userDto, ModelMap modelMap){

        logger.info("name-------------------->:{}",userConfig.getName());
        logger.info("username----------------->：{}",userConfig.getUsername());
        User user = new User();
        Wrapper<User> wrapper = new EntityWrapper<User>(user);
        if (StringUtils.isNotEmpty(userDto.getName())){
            wrapper.like("name",userDto.getName());
        }
        if (StringUtils.isNotEmpty(userDto.getUsername())){
            wrapper.eq("username",userDto.getUsername());
        }
        Page<User> page = new Page<User>(userDto.getPageNo(),userDto.getPageSize());

        long beginTime = System.currentTimeMillis();
        Page<User> userPages = userService.selectPage(page, wrapper);
        long endTime = System.currentTimeMillis();

        logger.info("项目启动成功后查询所用用户列表：{}", JSON.toJSONString(userPages));
        logger.info("查询时间维：{}毫秒", endTime-beginTime);
        modelMap.put("users",page);
        return  "/index";

    }


    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody ResultEntity getUserList(UserDto userDto){

        logger.info("name-------------------->:{}",userConfig.getName());
        logger.info("username----------------->：{}",userConfig.getUsername());
        User user = new User();
        Wrapper<User> wrapper = new EntityWrapper<User>(user);
        if (StringUtils.isNotEmpty(userDto.getName())){
            wrapper.like("name",userDto.getName());
        }
        if (StringUtils.isNotEmpty(userDto.getUsername())){
            wrapper.eq("username",userDto.getUsername());
        }
        Page<User> page = new Page<User>(userDto.getPageNo(),userDto.getPageSize());

        long beginTime = System.currentTimeMillis();
        Page<User> userPages = userService.selectPage(page, wrapper);
        long endTime = System.currentTimeMillis();

        logger.info("项目启动成功后查询所用用户列表：{}", JSON.toJSONString(userPages));
        logger.info("查询时间维：{}毫秒", endTime-beginTime);

        return ResultEntity.ok(userPages);
    }


    @RequestMapping(method = RequestMethod.GET,value = "/getRoleByUserID")
    public  @ResponseBody ResultEntity getRoleByUserID(UserDto userDto){
        if (userDto.getId()==null){
            return ResultEntity.error("id不允许为空");
        }
        List<User> userRoleByUID = userService.getUserRoleByUID(userDto.getId());
        return ResultEntity.ok(userRoleByUID);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    @ResponseBody
    public ResultEntity del(UserDto userDto){
        if (userDto.getId()==null){
            return ResultEntity.error("id不允许为空");
        }

        User user = new User();
        Wrapper<User> wrapper = new EntityWrapper<User>(user);
        userService.deleteById(userDto.getId());
        //logger.info("项目启动成功后查询所用用户列表：{}", JSON.toJSONString(users));
        return ResultEntity.ok("删除成功");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value = "/deleteBatchIds")
    public ResultEntity deleteBatchIds(List<Integer> integers){
        User user = new User();
        Wrapper<User> wrapper = new EntityWrapper<User>(user);
        userService.deleteBatchIds(integers);
        return ResultEntity.ok("批量删除成功");

    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public ResultEntity add(UserDto userDto){
        User user = new User();
        Wrapper<User> wrapper = new EntityWrapper<User>(user);
        if (StringUtils.isNotEmpty(userDto.getName())){
            wrapper.like("name",userDto.getName());
        }
        if (StringUtils.isNotEmpty(userDto.getUsername())){
            wrapper.eq("username",userDto.getUsername());
        }
        List<User> users = userService.selectList(wrapper);
        logger.info("项目启动成功后查询所用用户列表：{}", JSON.toJSONString(users));
        return ResultEntity.ok(users);
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public ResultEntity update(UserDto userDto){
        User user = new User();
        Wrapper<User> wrapper = new EntityWrapper<User>(user);
        if (StringUtils.isNotEmpty(userDto.getName())){
            wrapper.like("name",userDto.getName());
        }
        if (StringUtils.isNotEmpty(userDto.getUsername())){
            wrapper.eq("username",userDto.getUsername());
        }
        List<User> users = userService.selectList(wrapper);
        logger.info("项目启动成功后查询所用用户列表：{}", JSON.toJSONString(users));
        return ResultEntity.ok(users);
    }
}
